package com.teacold.customdemo.chapter_four;

import android.animation.Animator;
import android.animation.Keyframe;
import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.BounceInterpolator;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.teacold.customdemo.R;
import com.teacold.customdemo.chapter_three.CharEvaluator;

import androidx.appcompat.app.AppCompatActivity;

public class ProprtyValuesHolderActivity extends AppCompatActivity implements View.OnClickListener {

    private Button value_button;
    private TextView value_text, value_text2;
    private ObjectAnimator objectAnimator;
    private MyTextView value_myText;
    private LinearLayout value_ly;
    private int age;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_proprty_values_holder);
        initView();
    }

    private void initView() {
        value_button = findViewById(R.id.value_button);
        value_text = findViewById(R.id.value_text);
        value_text2 = findViewById(R.id.value_text2);
        value_myText = findViewById(R.id.value_myText);
        value_ly = findViewById(R.id.value_ly);

        value_button.setOnClickListener(this);
        value_text.setOnClickListener(this);
        addPropertyValuesHolder();
    }


    /**
     * 自定义ViewGroup的子View的入场出厂动画
     */
    private void addPropertyValuesHolder() {
        LayoutTransition transition = new LayoutTransition();
        PropertyValuesHolder pvhleft=PropertyValuesHolder.ofInt("left",0,0);
        PropertyValuesHolder pvhtop=PropertyValuesHolder.ofInt("top",0,0);
        PropertyValuesHolder pvhScaleX=PropertyValuesHolder.ofFloat("scaleX",1f,0f,1f);
        Animator changeAppearAnimtor=ObjectAnimator.ofPropertyValuesHolder(value_ly,pvhleft,pvhtop,pvhScaleX);
        transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING,changeAppearAnimtor);
        value_ly.setLayoutTransition(transition);
    }
    /**
     * 自定义ViewGroup的子View的入场出厂动画
     */
    private void addLayoutTransition() {
        LayoutTransition transition = new LayoutTransition();
        ObjectAnimator animOut = ObjectAnimator.ofFloat(null, "rotation", 0f, 90f, 0f);//入场
        ObjectAnimator animInput = ObjectAnimator.ofFloat(null, "rotationY", 0f, 360f, 0f);//出厂
        transition.setAnimator(LayoutTransition.DISAPPEARING, animOut);
        transition.setAnimator(LayoutTransition.APPEARING, animInput);
        value_ly.setLayoutTransition(transition);
    }


    private void addView() {
        age++;
        Button button = new Button(this);
        button.setText("添加" + age);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(params);
        value_ly.addView(button, 0);
    }

    private void removeButtonView() {
        if (age > 0) {
            value_ly.removeViewAt(0);
        }
        age--;
    }

    /**
     * ViewPropertyAnimator属性
     */
    private void viewPropertyAnimator() {
        value_text.animate().scaleY(2);
        value_text2.animate().scaleYBy(2);
    }

    /**
     * Keyframe 动画关键帧 Evaluator
     */
    private void ofKeyFrame() {
        Keyframe keyframe = Keyframe.ofObject(0f, new Character('A'));
        Keyframe keyframe1 = Keyframe.ofObject(0.1f, new Character('L'));
        Keyframe keyframe2 = Keyframe.ofObject(1f, new Character('Z'));
        PropertyValuesHolder propertyValuesHolder = PropertyValuesHolder.ofKeyframe("CharText", keyframe, keyframe1, keyframe2);
        propertyValuesHolder.setEvaluator(new CharEvaluator());
        objectAnimator = ObjectAnimator.ofPropertyValuesHolder(value_myText, propertyValuesHolder);
        objectAnimator.setDuration(5000);
    }

    /**
     * Keyframe 动画关键帧
     */
    private void ofKeyframe() {
        Keyframe keyframe = Keyframe.ofFloat(0f, 0);
        Keyframe keyframe1 = Keyframe.ofFloat(0.5f, 100f);
        Keyframe keyframe2 = Keyframe.ofFloat(1);
        keyframe.setValue(0f);
        keyframe2.setInterpolator(new BounceInterpolator());

        PropertyValuesHolder propertyValuesHolder = PropertyValuesHolder.ofKeyframe("rotation", keyframe, keyframe1, keyframe2);
        objectAnimator = ObjectAnimator.ofPropertyValuesHolder(value_text, propertyValuesHolder);
        objectAnimator.setDuration(1000);

    }

    /**
     * 实用ProperValuesHolder的ofObject()方法
     */
    private void ofObject() {
        PropertyValuesHolder obHolder = PropertyValuesHolder.ofObject("CharText", new CharEvaluator(), new Character('A'), new Character('Z'));
        objectAnimator = ObjectAnimator.ofPropertyValuesHolder(value_myText, obHolder);
        objectAnimator.setDuration(3000);
    }

    /**
     * 简单实用PropertyValuesHolder
     */
    private void initPropertyValuesHolder() {
        PropertyValuesHolder propertyValuesHolder = PropertyValuesHolder.ofFloat("Rotation", 60f, -60f, 40f, -40f, -20f, 20f, 10f, -10f, 0f);
        PropertyValuesHolder alphaHolder = PropertyValuesHolder.ofFloat("alpha", 0.1f, 1f, 0.1f, 1f);
        objectAnimator = ObjectAnimator.ofPropertyValuesHolder(value_text, propertyValuesHolder, alphaHolder);
        objectAnimator.setDuration(3000);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.value_button:
//                objectAnimator.start();
//                viewPropertyAnimator();
                addView();
                break;
            case R.id.value_text:
                removeButtonView();
                break;
        }
    }
}
